import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.util.Arrays;
import java.util.Locale;

/**
 * 自动生成 openapi，生成文件的位置在smart-doc.json中配置
 * @author 楚孔响
 * @version 1.0
 * @date 2022/1/11 16:39
 */
@Slf4j
public class ApiGenerator {

    public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
    public static final String BASE_PATH = System.getProperty("user.dir");
    public static final String MUDDLE_NAME = "simple-gateway";
    public static final String MAVEN_COMMAND = "mvn -X smart-doc:openapi -Dfile.encoding=UTF-8  -pl :" + MUDDLE_NAME + " -am";

    public static void main(String[] args) throws IOException {
        // 生成openapi的json文件
        getJson();

        // 读smart-doc配置文件，得到openapi的生成路径
        File configurationFile = ResourceUtils.getFile("./" + MUDDLE_NAME + "/src/main/resources/smart-doc.json");
        FileReader configurationFileReader = new FileReader(configurationFile);
        JSONReader configurationJsonReader = new JSONReader(configurationFileReader);
        JSONObject config = configurationJsonReader.readObject(JSONObject.class);
        configurationJsonReader.close();
        configurationFileReader.close();

        String dstPath = config.getString("outPath");
        dstPath += "/openapi.json";

        // 读openapi文件
        configurationFile = new File(dstPath);
        configurationFileReader = new FileReader(configurationFile);
        StringBuilder sb = new StringBuilder();
        int i;
        while ((i = configurationFileReader.read()) != -1) {
            sb.append((char) i);
        }
        configurationFileReader.close();
        String openapiContent = sb.toString();

        // 将读到的openapi推送至目标服务器
        // TODO

    }


    public static void getJson() throws IOException {
        execCommand();
    }

    public static void execCommand() throws IOException {
        log.info("开始生成openapi...");
        String[] commands = new String[0];
        if (OS_NAME.startsWith("windows")) {
            commands = new String[] { "cmd", "/c", MAVEN_COMMAND };
        } else if (OS_NAME.startsWith("linux")) {
            commands = new String[] { "/bin/bash", "-c", MAVEN_COMMAND };
        } else {
            System.err.println("不支持的操作系统");
            System.exit(0);
        }
        log.info("执行命令：{}", String.join(" ", Arrays.asList(commands)));

        Process process = new ProcessBuilder(commands).start();
        BufferedInputStream inputStream = new BufferedInputStream(process.getInputStream());
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line;
        while (bufferedReader.read() != -1) {
            line = bufferedReader.readLine();
            System.out.println(line);
        }
        bufferedReader.close();
        inputStream.close();

        // 读文件
        JSONReader jsonReader = new JSONReader(new FileReader(ResourceUtils.getFile("./" + MUDDLE_NAME + "/src/main/resources/smart-doc.json")));
        Object readObject = jsonReader.readObject();
    }

}
